package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private UserService userService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        orderService.submit(orders);
     return R.success("下单成功");
    }
    @GetMapping("/page")
    public R<Page<Orders>> page(int page,int pageSize,String number,String beinTime,String endTime){
        Page<Orders> ordersPage=new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage=new Page<>();
        LambdaQueryWrapper<Orders> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.between(StringUtils.isNotEmpty(beinTime),Orders::getOrderTime,beinTime,endTime);
        lambdaQueryWrapper.like(number!=null,Orders::getNumber,number);
        lambdaQueryWrapper.orderByAsc(Orders::getOrderTime);
        orderService.page(ordersPage,lambdaQueryWrapper);
//        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");
//        List<Orders> records = ordersPage.getRecords();
//        List<OrdersDto> list=records.stream().map(item->{
//            OrdersDto ordersDto=new OrdersDto();
//            BeanUtils.copyProperties(item,ordersDto);
//           /* Long userId = item.getUserId();
//            User userServiceById = userService.getById(userId);
//            if (userServiceById!=null){
//                String name = userServiceById.getName();
//                ordersDto.setUserName(name);
//            }*/
//            return ordersDto;
//        }).collect(Collectors.toList());
//        ordersDtoPage.setRecords(list);
        return R.success(ordersPage);
    }
    /**
     *订单明细
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> getByPage(int page,int pageSize){
        Page<Orders> pageInfo=new Page(page,pageSize);
        Page<OrdersDto>dtoPage=new Page<>();
        LambdaQueryWrapper<Orders> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Orders::getUserId,BaseContext.getCurrentId());
        lambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo,lambdaQueryWrapper);
        BeanUtils.copyProperties(pageInfo,dtoPage,"record");
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list=records.stream().map(item->{
            OrdersDto ordersDto=new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);
            Long id = item.getId();
            LambdaQueryWrapper<OrderDetail> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(OrderDetail::getOrderId,id);
            List<OrderDetail> detailList = orderDetailService.list(lambdaQueryWrapper1);
            ordersDto.setOrderDetails(detailList);
            return ordersDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }
    @PutMapping
    public R<String> updateById(@RequestBody Orders orders){
        Long id = orders.getId();
        Orders byId = orderService.getById(id);
        if (byId!=null){
            orders.setStatus(orders.getStatus());
            orderService.updateById(orders);
            return R.success("修改成功");
        }
        return R.error("修改错误");
    }
    @PostMapping("/again")
    public R<String> again(@RequestBody  Orders orders){
        Long id = orders.getId();
        LambdaQueryWrapper<OrderDetail> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderDetail::getOrderId,id);
        List<OrderDetail> list = orderDetailService.list(lambdaQueryWrapper);
        List<ShoppingCart> shoppingCarts=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            OrderDetail orderDetail = list.get(i);
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setName(orderDetail.getName());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setSetmealId(orderDetail.getSetmealId());
            shoppingCart.setAmount(orderDetail.getAmount());
            shoppingCart.setImage(orderDetail.getImage());
            shoppingCart.setDishFlavor(orderDetail.getDishFlavor());
            shoppingCart.setDishId(orderDetail.getDishId());
            shoppingCart.setNumber(orderDetail.getNumber());
            shoppingCarts.add(shoppingCart);
        }
        shoppingCartService.saveBatch(shoppingCarts);
        return R.success("再来一单成功");
    }
}
